from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

from Image import *

from soccerbots.core.structs import *

import math

textures = None
ActualRobot = None

def _main_cam():
    gluLookAt(0, -1100, 2000,
              0, 900, 0,
              0.0, 1.0, 1.0)


actual_cam = _main_cam
actual_state = None


def getRobot(team, player):
    ll = [actual_state.teamA,actual_state.teamB]
    return ll[team][TeamState.keyset[player]]

def SetCamera(team, player):
    global ActualRobot
    global actual_cam
    print "Set Camera"
    
    if ( team == -1 or actual_state == None):
        print ("camera general..")
        actual_cam = _main_cam
    else :
        print "camera robot.."
        ActualRobot = (team, player)
        actual_cam = _robot_cam        
        
def _robot_cam():
    # recuperar segons ActualRobot
    robot = getRobot(ActualRobot[0],ActualRobot[1])
    angle = robot.direction
    if robot.name == 'goalkeeper' :
        angle = angle + 90
    x = robot.position[0]
    y = robot.position[1]
    xa = math.cos(math.pi * angle / 180.0)
    ya = math.sin(math.pi * angle / 180.0)

    gluLookAt(x-(xa*50), y-(ya*50), 120,
              x+(xa*100), y+(ya*100), 90,
              0.0, 0.0, 1.0)
    
def SetState(state):
    global actual_state
    actual_state = state

def DrawBall(ball):
    mida = 21.35
    glPushMatrix()
    glBindTexture(GL_TEXTURE_2D, textures[3])
    glTranslatef(ball.position[0], ball.position[1], mida)
    glutSolidSphere(mida, 20, 20)
    glPopMatrix()
 
def DrawRobot(robot, c):
    mida = 75
    glPushMatrix()
    glTranslatef(robot.position[0],robot.position[1],mida/2)
    glRotatef(robot.direction, 0,0,1)
    glBindTexture(GL_TEXTURE_2D, textures[c])
    glScale(mida,mida,mida)
    glBegin(GL_QUADS)
    glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, -0.5, -0.5)
    glTexCoord2f(1.0, 0.0); glVertex3f( 0.5, -0.5, -0.5)
    glTexCoord2f(1.0, 1.0); glVertex3f( 0.5,  0.5, -0.5)
    glTexCoord2f(0.0, 1.0); glVertex3f(-0.5,  0.5, -0.5)
    
    glTexCoord2f(1.0, 0.0); glVertex3f(-0.5, -0.5, 0.5)
    glTexCoord2f(1.0, 1.0); glVertex3f( 0.5, -0.5, 0.5)
    glTexCoord2f(0.0, 1.0); glVertex3f( 0.5,  0.5, 0.5)
    glTexCoord2f(0.0, 0.0); glVertex3f(-0.5,  0.5, 0.5)

    glTexCoord2f(0.0, 0.0); glVertex3f(0.5,  0.5, -0.5)
    glTexCoord2f(1.0, 0.0); glVertex3f(0.5, -0.5, -0.5)
    glTexCoord2f(1.0, 1.0); glVertex3f(0.5, -0.5,  0.5)
    glTexCoord2f(0.0, 1.0); glVertex3f(0.5,  0.5,  0.5)

    glTexCoord2f(0.0, 0.0); glVertex3f(-0.5,  0.5, -0.5)
    glTexCoord2f(1.0, 0.0); glVertex3f(-0.5, -0.5, -0.5)
    glTexCoord2f(1.0, 1.0); glVertex3f(-0.5, -0.5,  0.5)
    glTexCoord2f(0.0, 1.0); glVertex3f(-0.5,  0.5,  0.5)

    glTexCoord2f(0.0, 0.0); glVertex3f( 0.5, 0.5, -0.5)
    glTexCoord2f(1.0, 0.0); glVertex3f(-0.5, 0.5, -0.5)
    glTexCoord2f(1.0, 1.0); glVertex3f(-0.5, 0.5,  0.5)
    glTexCoord2f(0.0, 1.0); glVertex3f( 0.5, 0.5,  0.5)
    
    glTexCoord2f(0.0, 0.0); glVertex3f( 0.5, -0.5, -0.5)
    glTexCoord2f(1.0, 0.0); glVertex3f(-0.5, -0.5, -0.5)
    glTexCoord2f(1.0, 1.0); glVertex3f(-0.5, -0.5,  0.5)
    glTexCoord2f(0.0, 1.0); glVertex3f( 0.5, -0.5,  0.5)
    
    glEnd()
    glPopMatrix()

def DrawState():
    if actual_state:
        DrawBall(actual_state.ball)
        for team in [actual_state.teamA, actual_state.teamB]:
            for player in TeamState.keyset:
                DrawRobot(team[player], team.teamId)

def LoadTexture(texture, filename):
    image = open(filename)
    ix = image.size[0]
    iy = image.size[1]
    image = image.tostring("raw", "RGBX", 0, -1)

    glBindTexture(GL_TEXTURE_2D, texture)
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
    glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)

def LoadTextures():
    global textures
    textures = glGenTextures(4)
    LoadTexture(textures[0], "soccerbots/newgui/imgs/camp.bmp")
    LoadTexture(textures[1], "soccerbots/newgui/imgs/red.bmp")
    LoadTexture(textures[2], "soccerbots/newgui/imgs/blue.bmp")
    LoadTexture(textures[3], "soccerbots/newgui/imgs/ball.bmp")
   
def InitGlut():
    glutInit([])

def InitGL(Width, Height):
    LoadTextures()

    glEnable(GL_LIGHTING)
    glEnable(GL_TEXTURE_2D)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClearDepth(1.0)
    glDepthFunc(GL_LESS)
    glEnable(GL_DEPTH_TEST)
    glShadeModel(GL_SMOOTH)
    
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)

    glLightfv(GL_LIGHT0, GL_AMBIENT, (0.5, 0.5, 0.5, 1.0))
    glLightfv(GL_LIGHT0, GL_DIFFUSE, (1.0, 1.0, 1.0, 1.0))
    glLightfv(GL_LIGHT0, GL_POSITION, (0.0, 0.0, 40.0, 1.0))
    glLightModel(33272, 33274)
    glEnable(GL_LIGHT0) 

def ReSizeGLScene(Width, Height):
    glViewport(0, 0, Width, Height)

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 10000.0)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()


def DrawGround():
    glBindTexture(GL_TEXTURE_2D, textures[0])
    glBegin(GL_QUADS)
    glTexCoord2f(0.0, 0.0); glVertex3f(-1250, 0, 0)
    glTexCoord2f(1.0, 0.0); glVertex3f( 1250, 0, 0)
    glTexCoord2f(1.0, 1.0); glVertex3f( 1250, 1800, 0)
    glTexCoord2f(0.0, 1.0); glVertex3f(-1250, 1800, 0)
    glEnd()

def RestoreCam():
    global actual_cam
    actual_cam = _main_cam

def RobotCam(id):
    pass

def DrawGLScene():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    '''
    glLightfv(GL_LIGHT0, GL_AMBIENT, (0.5, 0.5, 0.5, 1.0))
    glLightfv(GL_LIGHT0, GL_DIFFUSE, (1.0, 1.0, 1.0, 1.0))
    glLightfv(GL_LIGHT0, GL_POSITION, (0.0, 0.0, 40.0, 1.0))
    '''
    glEnable(GL_LIGHT0) 
    glLoadIdentity()

    actual_cam()
    DrawGround()
    DrawState()
